/**
 * 
 */
package robot.arm;

import com.sun.j3d.utils.geometry.*;

import com.sun.j3d.utils.universe.*;

import javax.media.j3d.*;

import javax.vecmath.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 
 *
 */
public class Arm {
	
	private Base theBase;
	private Wrist theWrist;
	private Shoulder theShoulder;
	private ArrayList<Joint> listOfJoints; // since these will be the items that are moved, we will make transform groups based around them
	private ArrayList<Link> mapOfLinks; // This will make it easy to find the Link for modifications. 
	private EndEffector endEffector;
	
	// In each case, the number of Links will be placed in a default position
	public Arm(float baseHeight, float shoulderLength, float wristDimensions, int numberOfLinks) {
		
		theBase = new Base(baseHeight);
		
		mapOfLinks = new ArrayList();
		
		listOfJoints = new ArrayList();
		
		theShoulder = new Shoulder(shoulderLength);
		theWrist = new Wrist(wristDimensions);
		endEffector = new EndEffector(0.5f);
		
		BaseJoint baseJoint = new BaseJoint();
		
		listOfJoints.add(baseJoint);
		
		ShoulderJoint shoulderJoint = new ShoulderJoint();
		
		listOfJoints.add(shoulderJoint);
		
		ElbowJoint elbowJoint = new ElbowJoint();
		
		listOfJoints.add(elbowJoint);
		
		WristJoint wristJoint = new WristJoint();
		
		listOfJoints.add(wristJoint);
		
		mapOfLinks.add(theBase);
		mapOfLinks.add(theShoulder);
		mapOfLinks.add(theWrist);
		mapOfLinks.add(endEffector);
	

	}
	
	public BranchGroup draw() {
		
		BranchGroup newBranch = new BranchGroup();
		
		   TransformGroup animateTransform = new TransformGroup();

		   animateTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		   animateTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		
		// first, set positions
		for(int i=0; i < listOfJoints.size();i++) {
			
		}
		
		Object[] linkArray = mapOfLinks.toArray();
		
		ArrayList<TransformGroup> smallestGroups = new ArrayList();
	
		
		for(int i=0; i < linkArray.length;i++) {
			
			if(!((Link)linkArray[i]).getName().equalsIgnoreCase("base") || !((Link)linkArray[i]).getName().equalsIgnoreCase("end effector")) {
			Cylinder tempCylinder = ((Link)linkArray[i]).getShape();
			Vector3f tempPos = ((Link)linkArray[i]).getPosition();
			
		      TransformGroup tg = new TransformGroup();
		      tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		      tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

		      Transform3D transform = new Transform3D();
		      
		      //Vector3f vector = new Vector3f( x, .0f, .0f);

		      transform.setTranslation(tempPos);

		      tg.setTransform(transform);

		      tg.addChild(tempCylinder);
		      
		      smallestGroups.add(tg);
			}
			else if(((Link)linkArray[i]).getName().equalsIgnoreCase("base")) {
				
			}
			else if(((Link)linkArray[i]).getName().equalsIgnoreCase("base")) {
				
			}
			
		}
		
		for (int i=0;i<smallestGroups.size();i++) {
			animateTransform.addChild(smallestGroups.get(i));
		}
		
		// second, set transforms for the animations for each section and subsection
		for(int i=0; i < listOfJoints.size();i++) {
			
		}
		
		// put the end effector as part of its own transform group so it moves in unison
		
		//listOfLinks = new Link[numberOfLinks];
		
		newBranch.addChild(animateTransform);
		
		   BoundingSphere bounds =

			      new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

			   Color3f light1Color = new Color3f(1.0f, 0.0f, 0.2f);

			   Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);

			   DirectionalLight light1

			      = new DirectionalLight(light1Color, light1Direction);

			   light1.setInfluencingBounds(bounds);

			   newBranch.addChild(light1);

			   // Set up the ambient light

			   Color3f ambientColor = new Color3f(1.0f, 1.0f, 1.0f);

			   AmbientLight ambientLightNode = new AmbientLight(ambientColor);

			   ambientLightNode.setInfluencingBounds(bounds);

			   newBranch.addChild(ambientLightNode);
		
		return newBranch;
	}
	


	public Arm() {
		
	}
	
	

}
